<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入文章标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" style="width: 308px">
        <el-date-picker
            v-model="daterangePublishTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="发布者" prop="publishBy">
        <el-input
            v-model="queryParams.publishBy"
            placeholder="请输入发布者"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章状态" prop="status" style="width: 200px;">
        <el-select v-model="queryParams.status" placeholder="请选择文章状态" clearable>
          <el-option
              v-for="dict in article_statuses"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="观看量" prop="viewCount">
        <el-input
            v-model="queryParams.viewCount"
            placeholder="请输入观看量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点赞量" prop="likeCount">
        <el-input
            v-model="queryParams.likeCount"
            placeholder="请输入点赞量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收藏量" prop="collectCount">
        <el-input
            v-model="queryParams.collectCount"
            placeholder="请输入收藏量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="articlesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="articleId"/>
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="文章封面" align="center" prop="coverImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="文章描述" align="center" prop="description"/>
      <el-table-column label="提交者" align="center" prop="publishBy"/>
      <el-table-column label="提交时间" align="center" prop="publishTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="article_statuses" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="info" icon="View" @click="handleView(scope.row)">详 情</el-button>
          <el-button link type="success" icon="Check" @click="handlePass(scope.row)">通 过</el-button>
          <el-button link type="danger" icon="Close" @click="handleFail(scope.row)">不通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改文章管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="articlesRef" :model="form" label-width="80px" disabled="disabled">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题"/>
        </el-form-item>
        <el-form-item label="文章封面" prop="coverImage">
          <image-upload v-model="form.coverImage"/>
        </el-form-item>
        <el-form-item label="文章描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入文章描述"/>
        </el-form-item>
        <el-form-item label="文章内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="不通过原因" v-model="openDialog" width="500px" append-to-body>
      <el-form ref="passFailRef" :model="form" label-width="120px">
        <el-form-item label="编号">
          <el-input v-model="form.articleId" disabled/>
        </el-form-item>
        <el-form-item label="文章标题">
          <el-input v-model="form.title" disabled/>
        </el-form-item>
        <el-form-item label="不通过原因">
          <el-input v-model="form.rejectReason" placeholder="请输入不通过原因"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlePassFail">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Review">
import {changeArticleStatus, getArticles, getReviewArticles} from "@/api/articles/articles";
import {parseTime} from "@/utils/ruoyi.js";
import {ref} from "vue";

const {proxy} = getCurrentInstance();
const {article_statuses} = proxy.useDict('article_statuses');

const articlesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangePublishTime = ref([]);
const status = ref('');
const openDialog = ref(false);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    description: null,
    publishTime: null,
    publishBy: null,
    status: null,
    viewCount: null,
    likeCount: null,
    collectCount: null,
  },
});

const {queryParams, form} = toRefs(data);

/** 查询文章管理列表 */
function getList() {
  loading.value = true;

  getReviewArticles(queryParams.value).then(response => {
    articlesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  })
      .finally(() => {
        loading.value = false;
      });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangePublishTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.articleId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

// 表单重置
function reset() {
  form.value = {
    articleId: null,
    title: null,
    coverImage: null,
    description: null,
    content: null,
    publishTime: null,
    publishBy: null,
    status: null,
    rejectReason: null,
    viewCount: null,
    likeCount: null,
    collectCount: null,
    createdBy: null,
    createdTime: null,
    updatedBy: null,
    updatedTime: null,
    delFlag: null
  };
  proxy.resetForm("articlesRef");
}

/** 查看按钮操作 */
function handleView(row) {
  reset();
  const _articleId = row.articleId || ids.value
  getArticles(_articleId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看文章详情";
  });
}

// 通过
function handlePass(row) {
  proxy.$modal.confirm(`是否通过"${row.title}"的文章？`)
      .then(() => {
        return changeArticleStatus(row.articleId, '2', null) // 明确传参
      })
      .then(res => {
        if (res.code === 200) { // 假设成功返回200
          proxy.$modal.msgSuccess("审核成功");
          getList();
        } else {
          proxy.$modal.msgError(res.msg || "操作失败");
        }
      })
}

// 不通过
function handleFail(row) {
  console.log("row", row)
  openDialog.value = true;
  // 打开dialog后回显表单值
  form.value = {
    articleId: row.articleId,
    title: row.title,
    rejectReason: null
  };
}

// 确认不通过
function handlePassFail() {
  if (!form.value.rejectReason) {
    return proxy.$modal.msgWarning("请填写不通过原因");
  }

  proxy.$modal.confirm(`确定拒绝通过 ${form.value.title} 的文章？`)
      .then(() => {
        return changeArticleStatus(
            form.value.articleId,
            '4',
            form.value.rejectReason
        )
      })
      .then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess("操作成功");
          openDialog.value = false;
          getList();
        }
      })
      .catch(() => {
        openDialog.value = false;
      });
}

onMounted(() => {
  getList();
})
</script>
